package com.taomu.app.zongchouhuli.moudle.index.contract.impl;

import android.util.Log;

import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.res.ResImportNoticeEntity;
import com.taomu.app.zongchouhuli.http.res.ResLunBoEntity;
import com.taomu.app.zongchouhuli.http.res.ResMainProductListEntity;
import com.taomu.app.zongchouhuli.http.res.ResNoticeEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductEntity;
import com.taomu.app.zongchouhuli.http.res.ResUpdateInfoEntity;
import com.taomu.app.zongchouhuli.moudle.index.contract.ProductContract;
import com.taomu.app.zongchouhuli.moudle.index.entity.NoticeEntity;
import com.taomu.app.zongchouhuli.moudle.index.entity.ProductEntity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

public class ProductPresenter implements ProductContract.Presenter {

    private ProductContract.View mContractView;

    private CompositeDisposable disposable;

    public ProductPresenter(ProductContract.View mContractView){
        this.mContractView = mContractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getBannerInfo() {

    }

    @Override
    public void getNoticeList() {
        Request.getIntence().getNoticeList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<ResNoticeEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResNoticeEntity resProductEntities) {
                        Log.d("wqdqwdwqdwqdwq","onNext:"+resProductEntities.getData().size());
                        if(mContractView != null){
                            mContractView.setNoticeList(resProductEntities);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        Log.d("wqdqwdwqdwqdwq","onError:"+e.getMessage());
                    }
                });

    }

    @Override
    public void getNoticeContent(String id) {
        Request.getIntence().getNoticeContent(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<String>(mContractView,disposable,true) {
                    @Override
                    public void onNext(String resProductEntities) {
                        if(mContractView != null){
                            mContractView.setNoticeContent(resProductEntities);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        Log.d("wqdqwdwqdwqdwq","onError:"+e.getMessage());
                    }
                });

    }

    @Override
    public void getNoticeInfo() {
        setTestViewFillperInfo();
    }

    @Override
    public void getHotInfo() {
        Request.getIntence().getMainPageDataList("is_hot")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<ResMainProductListEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResMainProductListEntity resProductEntities) {
                        Log.d("wqdqwdwqdwqdwq","onNext:"+resProductEntities.getData().size());
                        if(mContractView != null){
                            mContractView.setHotInfo(resProductEntities);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        Log.d("wqdqwdwqdwqdwq","onError:"+e.getMessage());
                    }
                });
    }

    @Override
    public void getLunboInfo() {
        Request.getIntence().getLunboInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<ResLunBoEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResLunBoEntity resProductEntities) {
                        if(mContractView != null){
                            mContractView.setLunboContent(resProductEntities);
                        }
                    }

                });
    }

    @Override
    public void getSuprmeInfo() {
        Request.getIntence().getMainPageDataList("is_good")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<ResMainProductListEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResMainProductListEntity resProductEntities) {
                        if(mContractView != null){
                            mContractView.setSuprmeInfo(resProductEntities);
                        }
                    }

                });
    }

    @Override
    public void getImportInfo() {
        Request.getIntence().getImportNoticeInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<ResImportNoticeEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResImportNoticeEntity resProductEntities) {
                        if(mContractView != null){
                            if(resProductEntities.getData() != null){
                                mContractView.setImportNotice(resProductEntities);
                            }
                        }
                    }
                });
    }

    @Override
    public void updateApp(String versioncode) {
        Request.getIntence().getUpdateApp(versioncode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<ResUpdateInfoEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResUpdateInfoEntity resProductEntities) {
                        if(mContractView != null){
                            if(resProductEntities.getData() != null){
                                mContractView.setUpdateApp(resProductEntities);
                            }
                        }
                    }
                });
    }

    @Override
    public void detch() {

    }

    private void setTestViewFillperInfo(){
        List<NoticeEntity> entities = new ArrayList<>();
        for(int i=0;i<5;i++){
            NoticeEntity entity = new NoticeEntity();
            entity.setContent("测试公告---------"+i);
            entities.add(entity);
        }
        if(mContractView != null){
            mContractView.setFillperInfo(entities);
        }
    }

    private void setHotInfo(){
        List<ProductEntity> entities = new ArrayList<>();
        for(int i=0;i<2;i++){
            ProductEntity entity = new ProductEntity();
            entity.setTitle("产品名称:::::"+i);
            if(i%2 == 0) {
                entity.setLimitNum("");
            }else{
                entity.setLimitNum("限量10件 8月3日截止");
            }
            entity.setPrice("¥666");
            entity.setIncome("最高收益3.25%");
            entities.add(entity);
        }
        if(mContractView != null){
            //mContractView.setHotInfo(entities);
        }
    }

    private void setSuprmeInfo(){
        List<ProductEntity> entities = new ArrayList<>();
        for(int i=0;i<4;i++){
            ProductEntity entity = new ProductEntity();
            entity.setTitle("产品名称:::::"+i);
            entity.setLimitNum("");
            entity.setPrice("¥666");
            entity.setIncome("最高收益3.25%");
            entities.add(entity);
        }
        if(mContractView != null){
            //mContractView.setSuprmeInfo(entities);
        }
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {
        mContractView = null;
        disposable = null;
    }
}
